import { ipcRenderer } from "electron";
import { Result } from "./result";

//-------------发送者----------------------
var invoke = function (action, params) {
  return new Promise((resolve) => {
    let json = JSON.stringify(params);
    ipcRenderer.invoke(action, json).then((r) => {
      resolve(JSON.parse(r));
    });
  });
};

var send = function (params) {
  return new Promise((resolve) => {
    let json = JSON.stringify(params);
    ipcRenderer.send("window.send", json);
    resolve(Result.ok("成功"));
  });
};

var revice = function (action, handle) {
  ipcRenderer.on("window.recive", (event, json) => {
    let params = JSON.parse(json);
    let act = params.action;
    if (act == action) {
      handle(params);
    }
  });
};

let IPC = {
  invoke,
  send,
  revice,
};

export default IPC;
